package mroute

import (
	"github.com/gin-gonic/gin"
	"mygo/mymongo"
	"mygo/mystruct"
	"encoding/json"
	"strconv"
	"mygo/global"
	"io/ioutil"
)

/* =================================================================================
   弹幕歌词接口
*/
// 弹幕对接函数
func MyBarrage_Api(cont *gin.Context) {
	switch cont.Request.Method {
		case "GET":
			id := cont.Query("id")
			_, ret := mymongo.MyMonDB.DanmakuCur.Danmaku_Get(id)
			cont.JSON(mystruct.StatusOK, gin.H{"code": 0, "data":ret.Data})
			
		case "POST":
			token := cont.PostForm("token")
			custom := cont.DefaultPostForm("custom", "")
			if custom == "" {
				cont.JSON(mystruct.StatusOK, gin.H{"code": 0, "data": ""})
				return
			}
			user := mymongo.Get_Chcae_User(token, global.Mtoken.Key)
			if user == nil {
				cont.JSON(mystruct.StatusOK, gin.H{"code": 302, "data": "/"})
				return
			}
			time_float64, _ := strconv.ParseFloat(cont.DefaultPostForm("time", "0.0"), 64)
			dict := &mystruct.Barrage_One{
				Author: cont.DefaultPostForm("author", "DIYgod"),
				Color: mystruct.StrToInt64(cont.DefaultPostForm("color", "16777215")),
				Id: cont.DefaultPostForm("id", ""),
				Text: cont.DefaultPostForm("text", ""),
				Time: time_float64,
				Type: mystruct.StrToInt64(cont.DefaultPostForm("type", "0")),
			}
			ret := mymongo.MyMonDB.DanmakuCur.Danmaku_Push(dict)
			cont.JSON(mystruct.StatusOK, gin.H{"code": 0, "data":ret})
	}
}
func MyBarrage_Model(cont *gin.Context) {
	model := cont.PostForm("model")
	id := cont.PostForm("id")
	switch model {
		case "add":
			var addall mystruct.Barrage_All
			jsonData, _ := ioutil.ReadAll(cont.Request.Body)
			json.Unmarshal(jsonData, &addall)
			ret := mymongo.MyMonDB.DanmakuCur.Danmaku_Add(&addall)
			cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})
		case "del":
			ret := mymongo.MyMonDB.DanmakuCur.Danmaku_Del(id)
			cont.JSON(mystruct.StatusOK, gin.H{"code": ret.Code, "msg": ret.Mesg})
	}
}